package com.jsj.controller;

import com.jsj.pojo.Borrow;
import com.jsj.pojo.BorrowHistory;
import com.jsj.service.BorrowReturnService;
import com.jsj.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class BorrowReturnController {

    @Autowired
    BorrowReturnService borrowReturnService;
    @Autowired
    HistoryService historyService;

//进入借入借出表的默认请求，查询全部借出信息
    @RequestMapping("/queryAllBorrow")
    @ResponseBody
    public List<Borrow> queryAllBorrow(){
        List<Borrow> borrowsList = borrowReturnService.queryAllBorrow();
        return borrowsList;
    }
    //未归还和已归还
    @RequestMapping("/queryBorrowByStatus")
    @ResponseBody
    public List<Borrow> queryBorrowByStatus(String borrowStatus){
        List<Borrow> borrowsList = borrowReturnService.queryBorrowByStatus(borrowStatus);
        return borrowsList;
    }


    //关键字搜索
    @RequestMapping("/queryBorrowByName")
    @ResponseBody
    public List<Borrow> queryBorrowByName(String deviceName){
        List<Borrow> borrowsList = borrowReturnService.queryBorrowByName(deviceName);
        System.out.println("----4-----"+borrowsList);
        return borrowsList;
    }

    //新增借出
    @RequestMapping("/addBorrow")
    @ResponseBody
    public Boolean addBorrow(int deviceId,String deviceType,String deviceName ,String borrowTime,
        String borrowName ,int borrowSno,String borrowClass,String borrowRemarks) throws ParseException {
        //增加到借出列表borrow

        Borrow borrow = new Borrow();
        borrow.setDeviceId(deviceId);
        borrow.setDeviceType(deviceType);
        borrow.setDeviceName(deviceName);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date borrowTime2 = format.parse(borrowTime);
        borrow.setBorrowTime(borrowTime2);
        borrow.setBorrowName(borrowName);
        borrow.setBorrowSno(borrowSno);
        borrow.setBorrowClass(borrowClass);
        borrow.setBorrowRemarks(borrowRemarks);
        System.out.println("borrow==========="+borrow);
        Boolean flag = borrowReturnService.addBorrow(borrow);

        System.out.println("----"+flag);
        return flag;
    }
//归还设备，归还时间，修改device_status状态
    @RequestMapping("/returnDeviceById")
    @ResponseBody
    public Boolean returnDeviceById(int borrowId, int deviceId, String returnTime) throws ParseException {
    Date returntime2=null;
        if (returnTime==null){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format1 = format.format(new Date());
            returntime2 = format.parse(format1);
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         returntime2 = format.parse(returnTime);

        System.out.println("-------------"+returnTime);
        System.out.println("-------------"+returntime2);
        Boolean flag = borrowReturnService.returnDevice(borrowId,deviceId,returntime2);
        return flag;
    }
    //修改借出信息
    @RequestMapping("/updateBorrowById")
    @ResponseBody
    public Boolean updateBorrowById(Borrow borrow){



        Boolean flag = borrowReturnService.updateBorrowById(borrow);
        return flag;
    }
 //删除借出信息
    @RequestMapping("/delBorrowById")
    @ResponseBody
    public Boolean delBorrowById(BorrowHistory borrowHistory){

        Boolean flag = borrowReturnService.delBorrowById(borrowHistory.getBorrowId());
        Boolean aBoolean = historyService.addBorrowHistory(borrowHistory);
        if (flag=true){
            if (aBoolean=true){
                return true;
            }
        }
        return false;
    }



}
